package com.aios.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aios.dao.UserGroupMapper;
import com.aios.entity.UserGroup;
import com.aios.entity.mapper.UserGroupHasUsers;
import com.aios.entity.unit.StatusEnum;
import com.aios.service.ExistValidationEnum;
import com.aios.service.UserGroupService;
import com.aios.util.Pager;

@Service("userGroupService")
public class UserGroupServiceImpl extends AbstractListService implements
		UserGroupService {

	@Autowired
	private UserGroupMapper userGroupMapper;

	@Override
	public List<UserGroup> findAll() {
		return userGroupMapper.findAllByStatus(0);
	}

	@Override
	public List<UserGroup> findAllNotDisabled() {
		return userGroupMapper.findAllByStatus(0);
	}

	@Override
	public List<UserGroup> findAllDisabled() {
		return userGroupMapper.findAllByStatus(1);
	}

	@Override
	public UserGroup findById(Integer id) {
		return userGroupMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(UserGroup entity) {
		return userGroupMapper.insert(entity);
	}

	@Override
	public int save(UserGroup entity, Integer systemUserID) {
		saveAddSettings(entity, systemUserID);
		return userGroupMapper.insert(entity);
	}

	@Override
	public int update(UserGroup entity) {
		return userGroupMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int update(UserGroup entity, Integer systemUserID) {
		saveEditSettings(entity, systemUserID);
		return userGroupMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(UserGroup entity) {
		return delete(entity.getId());
	}

	@Override
	public int delete(Integer id) {
		return userGroupMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<UserGroup> findAllByStatus(Integer status) {
		return userGroupMapper.findAllByStatus(status);
	}

	@Override
	public ExistValidationEnum validateUserGroupName(String userGroupName) {
		List<UserGroup> list = userGroupMapper.findByGroupName(userGroupName);

		if (list == null || list.size() == 0) {
			return ExistValidationEnum.NOT_EXIST;
		} else if (list.size() == 1) {
			return ExistValidationEnum.EXIST;
		} else {
			return ExistValidationEnum.MULTIPLE_EXIST;
		}
	}

	@Override
	public ExistValidationEnum validateUserGroupName(String groupName,
			Integer id) {
		List<UserGroup> list = userGroupMapper.findGroupNameNotWithId(groupName,id);
		
		if (list == null || list.size() == 0) {
			return ExistValidationEnum.NOT_EXIST;
		} else if (list.size() == 1) {
			return ExistValidationEnum.EXIST;
		} else {
			return ExistValidationEnum.MULTIPLE_EXIST;
		}
	}

	@Override
	public List<UserGroupHasUsers> findUsersForGroups(Integer status) {
		return userGroupMapper.findUsersForGroups(status);
	}

	@Override
	public Pager viewPaginatedActiveList(Integer start, Integer size) {
		prepare(start, size);
		try {
			pager.setTotal(userGroupMapper.countAllByStatus(StatusEnum.ACTIVE
					.getStatusValue()));
			pager.setList(userGroupMapper.findListPaginated(
					StatusEnum.ACTIVE.getStatusValue(), start, size));
		} catch (Exception e) {
			// logger
			e.printStackTrace();
		}

		return pager;
	}

	@Override
	public UserGroupHasUsers findByIdWithUsers(Integer id) {
		return userGroupMapper.findByIdWithUsers(id);
	}

	@Override
	public int getCount() {
		return userGroupMapper.countAllByStatus(0);
	}

}
